package com.evl.vehicle.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.evl.common.annotion.Permission;
import com.evl.common.constant.Const;
import com.evl.common.controller.BaseController;
import com.evl.common.exception.BizExceptionEnum;
import com.evl.common.exception.BussinessException;
import com.evl.common.persistence.dao.TicketKindMapper;
import com.evl.common.persistence.dao.TicketPriceMapper;
import com.evl.common.persistence.dao.VehicleMapper;
import com.evl.common.persistence.model.TicketKind;
import com.evl.common.persistence.model.TicketPrice;
import com.evl.common.persistence.model.Vehicle;
import com.evl.constant.TrainSeatType;
import com.evl.core.util.ToolUtil;
import com.evl.vehicle.service.IVehicleService;

/**
 * 车次管理控制器
 *
 * @author 
 * @Date 2017-06-30 15:06:53
 */
@Controller
@RequestMapping("/vehicle")
public class VehicleController extends BaseController {
	@Resource
	VehicleMapper vehicleMapper;
	
	@Resource
	TicketPriceMapper  ticketPriceMapper;
	
	@Resource
	TicketKindMapper ticketKindMapper;
	
	@Resource
	IVehicleService vehicleService;
	
    private String PREFIX = "/vehicle/";

    /**
     * 跳转到车次管理首页
     */
   	@RequestMapping("")
    public String index() {
        return PREFIX + "vehicle.html";
    }

    /**
     * 跳转到添加车次管理
     */
    @RequestMapping("/vehicle_add")
    public String vehicleAdd() {
        return PREFIX + "vehicle_add.html";
    }

    /**
     * 跳转到修改车次管理
     */
    @RequestMapping("/vehicle_update/{vehicleId}")
    public String vehicleUpdate(@PathVariable Integer vehicleId, Model model) {
    	Vehicle vehicle = this.vehicleMapper.selectById(vehicleId);
    	model.addAttribute(vehicle);
        return PREFIX + "vehicle_edit.html";
    }

    /**
     * 获取车次管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    @Permission(Const.ADMIN_NAME)
    public Object list(@RequestParam(required = false) String vehicleNo) {
        List<Vehicle> result = vehicleMapper.selectVehicles(super.getPara("vehicleNo"));
        return result;
    }

    /**
     * 新增车次管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Vehicle vehicle) {
    	if (ToolUtil.isEmpty(vehicle) || vehicle.getVehicleNo() == null) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
    	
    	return this.vehicleMapper.insert(vehicle);
    }

    /**
     * 删除车次管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer vehicleId) {
        return this.vehicleMapper.deleteById(vehicleId);
    }


    /**
     * 修改车次管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Vehicle vehicle) {
    	if (ToolUtil.isEmpty(vehicle) || vehicle.getId() == null) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
    	
    	return this.vehicleMapper.updateById(vehicle);
    }

    /**
     * 车次管理详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail() {
        return null;
    }
    
    /**
     * 跳转到车次管理--车站列表页面
     */
    @RequestMapping("/staion_detail/{vehicleId}")
    public String staionPage(@PathVariable Integer vehicleId, Model model) {
    	
    	Vehicle vehicle = this.vehicleMapper.selectById(vehicleId);
    	
    	model.addAttribute("vehicle", vehicle);
    	
        return "/station/station.html";
    }
    
    /**
     * 跳转到车次管理--车站列表页面
     */
    @RequestMapping("/trainSeat_detail/{vehicleId}")
    public String trainSeatPage(@PathVariable Integer vehicleId, Model model) {
    	
    	Vehicle vehicle = this.vehicleMapper.selectById(vehicleId);
    	
    	model.addAttribute("vehicle", vehicle);
    	
        return "/trainSeat/trainSeat.html";
    }
    
    
    /**
     * 跳转到车次管理--票价列表页面
     */
    @RequestMapping("/ticketPrice_detail/{vehicleId}")
    public String ticketPricePage(@PathVariable Integer vehicleId, Model model) {
    	
    	Vehicle vehicle = this.vehicleMapper.selectById(vehicleId);
    	
    	model.addAttribute("vehicle", vehicle);
    	
        return "/ticketPrice/ticketPrice.html";
    }
    
    /**
     * 车次管理--票价列表
     */
    @RequestMapping(value = "/ticketPrice/list/{vehicleId}")
    @ResponseBody
    public Object ticketPriceList(@PathVariable Integer vehicleId) {
    	Vehicle vehicle = this.vehicleMapper.selectById(vehicleId);
    	
		List<TicketPrice> ticketPrices= ticketPriceMapper.getTicketPrices(vehicle);
		for(TicketPrice tp : ticketPrices){
			if(tp.getStationId()!=null){
				tp.setVehicleNo(vehicle.getVehicleNo());
			}
			if(tp.getSeatType()!=null){
				tp.setSeatTypeStr(TrainSeatType.valueOf(Integer.parseInt(tp.getSeatType())));
			}
			if(tp.getKindId()!=null){
				TicketKind ticketKind = ticketKindMapper.selectByCode(tp.getKindId());
				if(ticketKind!=null){
					tp.setKindName(ticketKind.getName());
				}
			}
		}
		
		return ticketPrices;
    }
    
    
}
